<template>
  <div>
    <table-form :disabled="disabled" @next="step3Ctrl" @jump="jump">
      <template slot="title">
        <h2 class="fromDivTit">
          <div>请填写租金信息和上传附件</div>
          <i
            v-if="isEdit && detailRes.step <= 3"
            class="edit-icon"
            @click="isDisabled = true"
          />
        </h2>
      </template>
      <div class="form-body">
        <el-form
          ref="step3Form"
          :model="step3Form"
          label-width="65px"
          class="formDiv"
          :rules="rules"
        >
          <el-row :gutter="24">
            <el-col :xs="24" :sm="8" :lg="8">
              <el-form-item label="租金:" prop="rent">
                <el-input
                  v-model="step3Form.rent"
                  :disabled="isEdit && !isDisabled"
                  type="text"
                  placeholder="请输入"
                >
                  <i slot="suffix">澳币/周</i>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="8" :lg="8">
              <el-form-item label="押金:" prop="deposit">
                <el-input
                  v-model="step3Form.deposit"
                  :disabled="isEdit && !isDisabled"
                  type="text"
                  placeholder="请输入"
                >
                  <i slot="suffix">澳币</i>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :xs="24" :sm="8" :lg="8">
              <el-form-item label="起租日:" prop="beginDate">
                <el-date-picker
                  v-model="step3Form.beginDate"
                  :disabled="isEdit && !isDisabled"
                  type="date"
                  placeholder="起租日期"
                  style="width: 100%"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="8" :lg="8">
              <el-form-item label="截止日:">
                <el-date-picker
                  v-model="step3Form.endDate"
                  :disabled="isEdit && !isDisabled"
                  type="date"
                  placeholder="截止日期"
                  style="width: 100%"
                  :picker-options="endDateOptions"
                />
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="8" :lg="8">
              <el-form-item label="租期:">
                <el-input
                  v-model="step3Form.leaseTerm"
                  :disabled="isEdit && !isDisabled"
                  type="text"
                  placeholder="请输入(月)"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-form-item label="租客:" :required="true" label-width="75px">
              <div
                v-for="(item, index) in formArr"
                :key="index"
                class="fromDivArr"
              >
                <el-col :xs="24" :sm="4" :lg="4" class="agg">
                  <el-input
                    v-model="item.name"
                    :disabled="isEdit && !isDisabled"
                    type="text"
                    placeholder="租客姓名"
                  />
                </el-col>
                <el-col :xs="24" :sm="3" :lg="3" class="agg">
                  <el-select
                    v-model="item.gender"
                    :disabled="isEdit && !isDisabled"
                    placeholder="性别"
                    class="btnScr"
                  >
                    <el-option
                      v-for="it in options"
                      :key="it.value"
                      :label="it.label"
                      :value="it.value"
                    />
                  </el-select>
                </el-col>
                <el-col :xs="24" :sm="3" :lg="3" class="agg">
                  <el-input
                    v-model="item.country"
                    :disabled="isEdit && !isDisabled"
                    type="text"
                    placeholder="国籍"
                  />
                </el-col>
                <el-col :xs="24" :sm="3" :lg="4" class="agg">
                  <el-input
                    v-model="item.work"
                    :disabled="isEdit && !isDisabled"
                    type="text"
                    placeholder="工作"
                  />
                </el-col>
                <el-col :xs="24" :sm="4" :lg="4" class="agg">
                  <el-input
                    v-model="item.income"
                    :disabled="isEdit && !isDisabled"
                    type="text"
                    placeholder="收入"
                  />
                </el-col>
                <el-col :xs="24" :sm="3" :lg="3" class="agg">
                  <el-input
                    v-model="item.age"
                    :disabled="isEdit && !isDisabled"
                    type="number"
                    placeholder="年龄"
                  />
                </el-col>
                <el-col v-if="index == 0" :sm="4" :lg="3" class="agg addAgg">
                  <span
                    :class="isEdit && !isDisabled ? 'gray_line' : ''"
                    @click="addDct()"
                  >添加租客</span>
                </el-col>
                <el-col
                  v-if="index > 0"
                  :xs="24"
                  :sm="3"
                  :lg="3"
                  class="agg addAgg"
                >
                  <span @click="delDct(index)">删除</span>
                </el-col>
              </div>
            </el-form-item>
          </el-row>
          <el-row :gutter="32">
            <el-form-item
              :required="true"
              label="租约:"
              label-width="90px"
              class="form_up"
            >
              <el-col :xs="24" :sm="24" :lg="24" class="agg">
                <Upload
                  :id="id"
                  :maxcount="5"
                  :urls="step3Form.filePath"
                  :disabled="isEdit && !isDisabled"
                  tip="可上传PDF、图片(jpg/ png/ jpeg)"
                  format="pdf,jpg,png,jpeg"
                  @getImage="getAdImage"
                />
              </el-col>
            </el-form-item>
          </el-row>
          <el-row :gutter="32">
            <el-form-item label="备注:" label-width="75px">
              <el-col :xs="24" :sm="24" :lg="24" class="agg">
                <el-input
                  v-model="step3Form.remark"
                  :disabled="isEdit && !isDisabled"
                  type="textarea"
                  :rows="3"
                  placeholder="可备注是否有宠物，租客关系等"
                />
              </el-col>
            </el-form-item>
          </el-row>
        </el-form>
        <div v-if="jumpReason" class="jump-box">
          <div class="jump-reson">跳过原因:</div>
          <span class="span-box">{{ jumpReason }}</span>
        </div>
      </div>
      <span
        :class="isEdit ? 'gray_qx' : 'qx'"
        @click="openDialog(objMo)"
      >租客取消</span>
      <template v-if="isEdit" slot="footer">
        <div class="dialog-footer">
          <el-button
            v-if="isDisabled"
            :disabled="disabled"
            type="primary"
            @click="saveEdit"
          >保存</el-button>
        </div>
      </template>
    </table-form>
    <myModal :obj="objMo" @close="objMo.show = false" @save="renteCancel">
      <div class="mpdalBody">
        <p>确认取消？</p>
      </div>
    </myModal>

    <myModal :obj="nextObj" @close="nextObj.show = false" @save="nextObjSave">
      <div class="mpdalBody">
        <p>
          租金：{{ step3Form.rent }}澳币/周
          <br>
          起租日：{{ formatDot(step3Form.beginDate) }}
          <br>
          截止日：{{ formatDot(step3Form.endDate) }}
          <br>
          租期：{{ step3Form.leaseTerm }}个月
          <br>
        </p>
      </div>
    </myModal>
  </div>
</template>
<script>
import Upload from '@/components/Upload/SingleImage4'
import TableForm from '../tableForm.vue'
import { leaseStep3, leaseStep3Cancel, leaseDetail, edit } from '@/api/lease'
import myModal from '@/components/Modal'
import { formatDot } from '@/utils/common'
export default {
  components: {
    TableForm,
    Upload,
    myModal
  },
  props: {
    id: {
      type: String,
      default: ''
    },
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formatDot,
      isDisabled: false,
      disabled: false,
      rules: {
        rent: [{ required: true, message: '请输入租金', trigger: 'blur' }],
        deposit: [{ required: true, message: '请输入押金', trigger: 'blur' }],
        beginDate: [
          { required: true, message: '请选择起租日期', trigger: 'blur' }
        ]
      },
      step3Form: {},
      renterCancel: {
        processId: ''
      },
      options: [
        {
          value: 'MALE',
          label: '男'
        },
        {
          value: 'FEMALE',
          label: '女'
        }
      ],
      detailRes: {
        jumpList: []
      },
      formArr: [
        { name: '', gender: '', country: '', work: '', income: '', age: '' }
      ],
      objMo: {
        show: false,
        title: null,
        width: '300px'
      },
      nextObj: {
        show: false,
        title: '租约确认',
        width: '500px'
      },
      endDateOptions: {
        disabledDate: (time) => {
          return this.step3Form.beginDate >= time.getTime()
        }
      }
    }
  },
  computed: {
    jumpReason() {
      if (!this.detailRes.jumpList) return ''
      const hasReason = this.detailRes.jumpList.find((item) => {
        return item.jumpStep === 3
      })
      return hasReason ? hasReason.jumpNote : ''
    }
  },
  watch: {
    step3Form: {
      handler(newVal, oldVal) {
        var itemIsEmpty = false
        var { rent, beginDate, deposit, filePath } = this.step3Form
        this.formArr.forEach((item, index) => {
          if (
            Object.values(item).find((it) => it === '') === '' ||
            !this.formArr.length
          ) {
            itemIsEmpty = true
          }
        })
        if (itemIsEmpty || !rent || !beginDate || !deposit || !filePath) {
          this.disabled = true
        } else {
          this.disabled = false
        }
      },
      deep: true
    },
    formArr: {
      handler(n, o) {
        this.step3Form.renterList = n
      },
      deep: true
    }
  },
  created() {
    this.getLeaseDetail()

    this.step3Form = {
      rent: '',
      deposit: '',
      beginDate: '',
      endDate: '',
      leaseTerm: '',
      remark: '',
      id: this.id,
      renterList: [],
      filePath: '',
      stepName: '待上传租约'
    }
  },
  methods: {
    async getLeaseDetail() {
      var leaseRes = await leaseDetail(this.id)
      this.detailRes = leaseRes.data
      var list = leaseRes.data.recommendHistoryList
      var successList = list.filter((item, index) => {
        return item.status === 5
      })
      var item = successList[successList.length - 1] || {}
      this.step3Form = {
        rent: item.rent || this.detailRes.rent,
        deposit: item.deposit || this.detailRes.deposit,
        beginDate: item.beginDate || this.detailRes.beginDate,
        endDate: item.endDate || this.detailRes.endDate,
        leaseTerm: item.leaseTerm || this.detailRes.leaseTerm,
        remark: this.detailRes.remark,
        renterList: item.recommendRenterList || this.detailRes.renterList || [],
        filePath: this.detailRes.filePath || '',
        stepName: '待上传租约'
      }

      this.formArr = this.step3Form.renterList.length
        ? this.step3Form.renterList
        : [
          {
            name: '',
            gender: '',
            country: '',
            work: '',
            income: '',
            age: ''
          }
        ]
    },
    getAdImage(img) {
      this.step3Form.filePath = img
    },
    async saveEdit() {
      var res = await edit(this.step3Form)
      if (res.code === 200) {
        this.$message({ message: '操作成功', type: 'success' })
        this.isDisabled = false
      }
    },
    async step3Ctrl() {
      this.nextObj.show = true
      // return
      // this.step3Form.id = this.id
      // this.step3Form.renterList = this.formArr

      // this.$refs['step3Form'].validate(async(valid) => {
      //   if (valid) {
      //     var step3Res = await leaseStep3(this.step3Form)
      //     if (step3Res.code === 200) {
      //       this.$emit('next')
      //     }
      //   }
      // })
    },
    async nextObjSave() {
      this.step3Form.id = this.id
      this.step3Form.renterList = this.formArr

      this.$refs['step3Form'].validate(async(valid) => {
        if (valid) {
          var step3Res = await leaseStep3(this.step3Form)
          if (step3Res.code === 200) {
            this.$emit('next')
          }
        }
      })
    },
    async renteCancel() {
      await leaseStep3Cancel(this.id)
      this.step3Form = {
        rent: '',
        deposit: '',
        beginDate: '',
        endDate: '',
        leaseTerm: '',
        remark: '',
        id: this.id,
        renterList: [],
        filePath: '',
        stepName: '待上传租约'
      }
      this.$emit('next', 1)
    },
    jump() {
      const { renterList } = this.step3Form
      renterList.forEach((item, i) => {
        const allEmpty = Object.values(item).every((val) => !val)
        if (allEmpty) {
          renterList.splice(i, 1)
        }
      })
      this.step3Form.renterList = renterList.length > 0 ? renterList : null
      this.$emit('jump', this.step3Form)
    },
    openDialog(obj) {
      if (this.isEdit) return
      obj.show = true
    },
    addDct() {
      if (this.isEdit && !this.isDisabled) {
        return
      } else {
        this.formArr.push({
          name: '',
          gender: '',
          country: '',
          work: '',
          income: ''
        })
      }
    },
    addReturn() {
      return
    },
    delDct(par) {
      this.formArr.splice(par, 1)
    }
  }
}
</script>
<style lang="scss" scoped>
.form_up {
  margin-left: -16px;
}
.countFsColor {
  cursor: pointer;
}
.mpdalBody p {
  padding: 20px;
  text-align: center;
  font-size: 20px;
}
.myBnt {
  color: #5ac6b7;
  display: block;
  width: 100px;
  margin-left: 115px;
  margin-bottom: 20px;
  border: 0;
}
.divUl {
  margin: 0px auto 0 auto;
}
h5,
p {
  padding: 0;
  margin: 0;
}
.ulMy {
  flex-wrap: wrap;
  width: 166px;
  padding-left: 10px;
}
.ulMy li {
  width: 100%;
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  height: 40px;
  line-height: 40px;
}
.ulMy li i {
  color: #6a6a6a;
  font-size: 13px;
  align-self: self-start;
  margin-top: 23px;
}
.ulMy li p {
  color: #6a6a6a;
  font-size: 13px;
}
.ulMy li h5 {
  font-size: 16px;
  color: #151515;
  margin-bottom: 10px;
}
.ulMy li div {
  background: #fff;
  margin-left: 0px;
  flex: 1;
  padding: 0px;
  box-sizing: border-box;
  border-radius: 2px;
  text-align: center;
  font-size: 12px;
}
.ulMy li em {
  color: #5ac6b7;
  margin-left: 3px;
}
.ulMy li dfn {
  color: #5ac6b7;
  margin-top: 3px;
  font-size: 12px;
  display: block;
  margin-top: 10px;
}
.ulMy li.not {
  color: #ccc;
}
.ulMy li.not h5 {
  font-size: 16px;
  color: #ccc;
  margin-bottom: 10px;
}
.ulMy li.not div {
  background: #fff;
  margin-left: 0px;
  flex: 1;
  padding: 0px;
  box-sizing: border-box;
  border-radius: 2px;
}
.ulMy li.not p {
  color: #ccc;
}
.overview-content {
  min-height: calc(100vh - 160px);
  background: #eee;
}
.myDigo h2 {
  background: #343436;
}
::v-deep .myDigo .el-dialog__footer {
  background: #e5e9ed;
  padding-top: 15px;
}
::v-deep .el-dialog.myDigo .el-dialog__body {
  padding: 0;
}
::v-deep .el-dialog.myDigo .el-dialog__header {
  display: none;
}
.el-dialog.myDigo h2 {
  height: 58px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 39px;
  padding-right: 39px;
  color: #fff;
  font-size: 18px;
  padding-top: 0;
  margin-top: 0;
  overflow: hidden;
  font-weight: normal;
  font-family: "SourceHanSansSC-regular";
}
.el-dialog.myDigo h2 i {
  font-size: 26px;
  cursor: pointer;
}
::v-deep .el-dialog.myDigo .el-dialog__body {
  text-align: left;
}
.formDiv {
  width: 800px;
  padding-left: 1px;
  padding-right: 39px;
  background: #fff;
  overflow: hidden;
}
.divSE {
  text-align: center;
}
.divSE em {
  display: block;
}
.fileWD {
  font-size: 46px;
  color: #5ac6b7;
}
.titleDiv {
  background: #5ac6b7;
  padding-left: 20px;
  color: #fff;
  height: 60px;
  line-height: 60px;
}
.two {
  margin: 10px 0;
  font-size: 13px;
  padding-left: 10px;
  padding-right: 10px;
}
.overview-tabs {
  background: #6a6a6a !important;
}
.inputTop {
  display: flex;
  justify-content: space-between;
}
.el-upload__tip {
  display: inline-block;
}
.formdiv {
  width: calc(100% - 200px);
}
.p1 {
  font-size: 12px;
  color: #d0d0d0;
}
.addBntLease p {
  display: inline-block;
  width: 68px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  color: #5ac6b7;
  cursor: pointer;
  border: 1px solid #5ac6b7;
  border-radius: 3px;
  font-size: 14px;
  margin-right: 10px;
}
.ulLeaseBnt li {
  background: #f8fbfa;
  border-radius: 6px;
  margin-top: 10px;
  font-size: 12px;
  display: flex;
  box-sizing: border-box;
  padding: 20px;
  flex-wrap: wrap;
}
.ulLeaseBnt li div.left {
  color: #666;
  width: 110px;
  text-align: left;
}
.ulLeaseBnt li div.left p {
  margin-bottom: 10px;
}
.ulLeaseBnt li div.left p:nth-child(1) {
  color: #5ac6b7;
  font-size: 13px;
}
.ulLeaseBnt li div.right p i {
  display: block;
  margin-bottom: 8px;
}
.ulLeaseBnt li div.right dfn {
  margin-top: 20px;
  display: block;
  color: #5ac6b7;
}
.ulLeaseBnt li div.line {
  border-top: 1px solid #f0f0f0;
  display: block;
  width: 100%;
  margin-top: 13px;
  padding-top: 22px;
  padding-left: 110px;
  box-sizing: border-box;
}
.ulLeaseBnt li div.line dfn {
  margin-right: 10px;
  color: #5ac6b7;
}
.ulLeaseBnt li:last-child {
  margin-bottom: 20px;
}
.opdfn {
  position: absolute;
  right: -12px;
  top: 0px;
  z-index: 99;
}
.agg {
  padding-left: 8px !important;
  padding-right: 8px !important;
}
.addAgg {
  color: #5ac6b7;
}
.fromDivArr {
  margin-bottom: 10px;
  overflow: hidden;
}
.divZk {
  margin-bottom: 20px;
}
.divZk ul li {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}
.divZk ul li {
  color: #101010;
  font-size: 12px;
  width: 100%;
}
.divZk ul li i {
  color: #b3b3b3;
  width: 50px;
  text-align: right;
  display: inline-block;
  margin-right: 10px;
}
.divZk ul li p {
  width: 33.3%;
  text-align: left;
}
.divZk ul.auto li {
  width: auto;
}
.divZk ul.auto li {
  flex-wrap: wrap;
}
.divZk ul.auto li p {
  display: block;
  width: 100%;
  margin-bottom: 10px;
}
.divZk ul.auto li p em {
  margin-right: 20px;
}
.divZk ul li span {
  color: #5ac6b7;
}
.pAll {
  font-size: 12px;
  color: #5ac6b7;
}
.pAll span {
  margin-right: 15px;
}
.pAll span i {
  margin-left: 15px;
}
.qx {
  margin-left: 110px;
  color: #5ac6b7;
  font-size: 12px;
  cursor: pointer;
}
.gray_qx {
  font-size: 12px;
  margin-left: 110px;
  color: gray !important;
  cursor: not-allowed !important;
}
.edit-icon {
  cursor: pointer;
}
.gray_line {
  color: gray !important;
  // border: 1px solid gray!important;
  cursor: not-allowed !important;
}
.form-body {
  display: flex;
  .jump-box {
    width: 300px;
    margin-left: 0 !important;
    padding-left: 20px;
    word-break: break-all;
    .jump-reson {
      color: #5ac6b7;
      font-size: 16px;
      margin-bottom: 10px;
    }
  }
}
</style>
